﻿using SelfMadeMillionaire.Core.Entities;
using SelfMadeMillionaire.Core.Enums;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SelfMadeMillionaire.Core.Specifications.Publications
{
	public class HomePagePublicationsSpecification : ISearchSpecification<Publication>
	{
		private SubscriptionType _minAccessLevel;

		public HomePagePublicationsSpecification(SubscriptionType minAccessLevel)
		{
			_minAccessLevel = minAccessLevel;
		}

		public IEnumerable<Publication> SatisfiedBy(IQueryable<Publication> entities)
		{
			return entities.Where(x => x.ProductType == (byte)ProductType.SelfMadeMillionaire &&
				x.IsShowOnStartPage && 
				x.MinAccessLevel <= (byte)_minAccessLevel &&
				x.Status == (byte)StatusType.Active)
				.OrderByDescending(x => x.Date)
				.Take(4);
		}
	}
}
